package com.holystone.orca.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.mybatis.dao.SysLookupTypeMapper;
import com.holystone.orca.sys.mybatis.model.SysLookupCde;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.holystone.orca.base.service.impl.BaseServiceImpl;
import com.holystone.orca.sys.mybatis.model.SysLookupType;
import com.holystone.orca.sys.service.ISysLookupTypeService;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service(value="sysLookupTypeService")
public class SysLookupTypeServiceImpl extends BaseServiceImpl<SysLookupType> implements ISysLookupTypeService{

    private static Logger logger = Logger.getLogger(SysLookupTypeServiceImpl.class);

    @Autowired
    private SysLookupTypeMapper sysLookupTypeMapper;

    @Override
    public PageData pagingSysLookupType(int iPageNo, int iPageSize) {
        logger.debug(">>>pagingSysLookupType-->iPageNo:"+iPageNo+"iPageSize:"+iPageSize);
        Example  example =new Example(SysLookupType.class);
//        List<SysLookupType> list =  selectByExample(example);
        //分页查询
        PageHelper.startPage(iPageNo, iPageSize);
        PageInfo<SysLookupType> pageInfo = new PageInfo<SysLookupType>(sysLookupTypeMapper.selectByExample(example));
        PageData pageData =  new PageData();
        //List<SysLookupCdeVO> listCiType = pageInfo.getList();
        logger.debug("查询的结果集大小"+pageInfo.getList()+"，总页数"+pageInfo.getPages()+"，页大小"+pageInfo.getPageSize()+"，总条数"+pageInfo.getTotal());
        pageData.setPageList(pageInfo.getList());
        pageData.setTotalCount(pageInfo.getTotal());
        pageData.setPageSize(pageInfo.getPageSize());
        pageData.setPageNo(pageInfo.getPageNum());
        pageData.setTotalPage(pageInfo.getPages());
        logger.debug("<<<pagingSysLookupType");
        return pageData;
    }

    @Override
    public int saveOrUpdateLookupType(SysLookupType sysLookupType,String type) throws Exception {
        int result=0;
        if(type!=null && "add".equals(type) ){
            Example  example =new Example(SysLookupType.class);
            Example.Criteria cri = example.createCriteria();
            cri.andEqualTo("lookupType", sysLookupType.getLookupType());
            SysLookupType sysLookupType1= sysLookupTypeMapper.selectByPrimaryKey(sysLookupType.getLookupType());
            if(sysLookupType1==null){
                result=sysLookupTypeMapper.saveOrUpdateLookupType(sysLookupType);
            }else{
            	return result;
            }
        }else{
            result=sysLookupTypeMapper.saveOrUpdateLookupType(sysLookupType);
        }
        return result;
    }
}
